package com.ysd.lis.service.inv.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.inv.*;
import com.ysd.lis.mapper.inv.InvOrderReagentMapper;
import com.ysd.lis.request.inv.InvOrderParam;
import com.ysd.lis.request.inv.InvOrderReagentTransferParam;
import com.ysd.lis.service.inv.InvOrderReagentService;
import com.ysd.util.ToolsUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 订单明细表 服务实现类
 * </p>
 *
 * @author WH
 * @since 2024-09-19
 */
@Service
public class InvOrderReagentServiceImpl extends ServiceImpl<InvOrderReagentMapper, InvOrderReagent> implements InvOrderReagentService {

    @Autowired
    InvOrderReagentMapper invOrderReagentMapper;
    @Override
    public Result findOrderReagentList(InvOrderParam param) {
        MPJLambdaWrapper<InvOrderReagent> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(InvOrderReagent.class)
                .selectAs("ir", InvReagent::getMiniUnit, InvOrderReagent::getMiniUnit)
                .selectAs("ir", InvReagent::getMinTemp, InvOrderReagent::getMinTemp)
                .selectAs("ir", InvReagent::getMaxTemp, InvOrderReagent::getMaxTemp)
                .selectAs("ir", InvReagent::getStockCondition, InvOrderReagent::getStockCondition)
                .selectAs("ir", InvReagent::getBarcode, InvOrderReagent::getBarcode)
                .selectAs("ir", InvReagent::getOutUnit, InvOrderReagent::getOutUnit)
                .selectAs("ir", InvReagent::getIsBatchNumMgt, InvOrderReagent::getIsBatchNumMgt)
                .selectAs("ir", InvReagent::getIsSingleBarcode, InvOrderReagent::getIsSingleBarcode)
                .selectAs("ir", InvReagent::getExpirDay, InvOrderReagent::getExpirDay)
                .selectAs("ir", InvReagent::getExternalNo, InvOrderReagent::getExternalNo)
                .leftJoin(InvReagent.class, "ir",p->p.eq(InvReagent::getReaNo, InvOrderReagent::getReaNo).eq(InvReagent::getDelFlag, 0))
                .eq(InvOrderReagent::getOrderInfoId,param.getOrderInfoId())
                .eq(InvOrderReagent::getDelFlag,0)
                .orderByDesc(InvOrderReagent::getCreateTime);
        List<InvOrderReagent> itemList = invOrderReagentMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", itemList);
    }

    @Override
    public Result findInOutStorageDetail(InvOrderParam param) {
        String documentDateBegin = param.getDocumentDateBegin();
        String documentDateEnd = param.getDocumentDateEnd();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        MPJLambdaWrapper<InvOrderReagent> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAll(InvOrderReagent.class)
                .selectAs("o", InvOrderInfo::getTransferNo, InvOrderReagent::getTransferNo)
                .selectAs("o", InvOrderInfo::getTransferType, InvOrderReagent::getTransferType)
                .selectAs("o", InvOrderInfo::getDocumentDate, InvOrderReagent::getDocumentDate)
                .selectAs("imf", InvManufacturer::getManuName, InvOrderReagent::getManuName)

                .innerJoin(InvOrderInfo.class,"o",p->p.eq(InvOrderInfo::getId,InvOrderReagent::getOrderInfoId).eq(InvOrderInfo::getDelFlag,0))
                .innerJoin(InvTransferType.class,"tf",p->p.eq(InvTransferType::getTransferCode,InvOrderInfo::getTransferType).eq(InvTransferType::getBusinessType,param.getBusinessType()).eq(InvTransferType::getDelFlag,0))
                .leftJoin(InvReagent.class,"ir",p->p.eq(InvReagent::getReaNo,InvOrderReagent::getReaNo).eq(InvReagent::getDelFlag,0))
                .leftJoin(InvManufacturer.class,"imf",p->p.eq(InvManufacturer::getManuCode,InvReagent::getManufacturerNo).eq(InvManufacturer::getDelFlag,0));

        if (ToolsUtils.isNotEmpty(documentDateBegin)) {
            try {
                Date date = dateFormat.parse(documentDateBegin);
                Timestamp timestampBeg = new Timestamp(date.getTime());
                queryWrapper.ge(InvOrderInfo::getDocumentDate, timestampBeg);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        if (ToolsUtils.isNotEmpty(documentDateEnd)) {
            try {
                Date date = dateFormat.parse(documentDateEnd);
                Timestamp timestampEnd = new Timestamp(date.getTime());
                queryWrapper.le(InvOrderInfo::getDocumentDate, timestampEnd);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        queryWrapper.eq(InvOrderReagent::getDelFlag,0)
                .orderByDesc(InvOrderReagent::getCreateTime);
        List<InvOrderReagent> itemList = invOrderReagentMapper.selectList(queryWrapper);

        return Result.succ(1, "查询成功", itemList);
    }

    @Override
    public Result findOrderReagentByTransferType(InvOrderReagentTransferParam param) {

        String allotDateBegin = param.getAllotDateBegin();
        String allotDateEnd = param.getAllotDateEnd();
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        MPJLambdaWrapper<InvOrderReagent> queryWrapper = new MPJLambdaWrapper<>();
        queryWrapper.selectAs(InvOrderReagent::getReaNo, InvOrderReagent::getReaNo)
                .selectAs(InvOrderReagent::getReaNa, InvOrderReagent::getReaNa)
                .selectAs(InvOrderReagent::getCostPrice, InvOrderReagent::getCostPrice)
                .selectAs(InvOrderReagent::getSpecModel, InvOrderReagent::getSpecModel)
                .selectAs(InvOrderReagent::getUnit, InvOrderReagent::getUnit)
                .selectAs(InvReagent::getManufacturerNo, InvOrderReagent::getManufacturerNo)
                .selectAs(InvTransferType::getTransferName, InvOrderReagent::getTransferType)
                .selectAs(InvOrderInfo::getCreateTime, InvOrderReagent::getCreateTime)
                .selectSum(InvOrderReagent::getAmount, InvOrderReagent::getAmount)
                .selectSum(InvOrderReagent::getCosts, InvOrderReagent::getCosts)
                .innerJoin(InvOrderInfo.class,p->p.eq(InvOrderInfo::getId,InvOrderReagent::getOrderInfoId).eq(InvOrderInfo::getDelFlag,0).eq(InvOrderInfo::getOrderStatus,"10"))
                .innerJoin(InvReagent.class,p->p.eq(InvReagent::getReaNo,InvOrderReagent::getReaNo).eq(InvReagent::getDelFlag,0))
                .innerJoin(InvTransferType.class,p->p.eq(InvTransferType::getTransferCode,InvOrderInfo::getTransferType).eq(StringUtils.isNotBlank(param.getTransferType()),InvTransferType::getBusinessType,param.getTransferType()).eq(InvTransferType::getDelFlag,0));

        if (ToolsUtils.isNotEmpty(allotDateBegin) && ToolsUtils.isNotEmpty(allotDateEnd)) {
            try {
                Date begin = dateFormat.parse(allotDateBegin);
                Date end = dateFormat.parse(allotDateEnd);
                Timestamp timestampBeg = new Timestamp(begin.getTime());
                Timestamp timestampEng = new Timestamp(end.getTime());
                queryWrapper.ge(InvOrderInfo::getDocumentDate, timestampBeg);
                queryWrapper.le(InvOrderInfo::getDocumentDate, timestampEng);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        queryWrapper.eq(InvOrderReagent::getDelFlag,0)
                .groupBy(InvOrderReagent::getReaNo)
                .groupBy(InvOrderReagent::getReaNa)
                .groupBy(InvOrderReagent::getCostPrice)
                .groupBy(InvOrderReagent::getSpecModel)
                .groupBy(InvOrderReagent::getUnit)
                .groupBy(InvReagent::getManufacturerNo)
                .groupBy(InvTransferType::getTransferName)
                .groupBy(InvOrderInfo::getCreateTime);
        List<InvOrderReagent> itemList = invOrderReagentMapper.selectList(queryWrapper);
        return Result.succ(1, "查询成功", itemList);
    }
}
